﻿// --------------------------------
// <copyright file="Program.cs" company="Pattern Web Solutions">
//     Copyright © Simon Gill 2010. Distributed under the GPL.
// </copyright>
// <author>Simon Gill</author>
// ---------------------------------
// This file is part of League Manager

// League Manager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.

// League Manager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with League Manager.  If not, see <http://www.gnu.org/licenses/>.

namespace LeagueManager
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using System.Linq;
    using Models;
    using log4net.Repository;
    using log4net.Appender;
    using log4net;

    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
            SetDataDirectory();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm(new MembershipRepository()));
        }

        public static string GetUserDataPath()
        {
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            dir = Path.Combine(dir, Application.CompanyName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            return dir;
        }

        private static void SetDataDirectory()
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", GetUserDataPath());
            InitializeLog4NetPath(GetUserDataPath());
        }

        public static void InitializeLog4NetPath(string logDirectory)
        {
            //get the current logging repository for this application
            ILoggerRepository repository = LogManager.GetRepository();
            //get all of the appenders for the repository
            IAppender[] appenders = repository.GetAppenders();
            //only change the file path on the 'FileAppenders'
            foreach (IAppender appender in (from iAppender in appenders
                                            where iAppender is FileAppender
                                            select iAppender))
            {
                FileAppender fileAppender = appender as FileAppender;
                //set the path to your logDirectory using the original file name defined
                //in configuration
                fileAppender.File = Path.Combine(logDirectory, Path.GetFileName(fileAppender.File));
                //make sure to call fileAppender.ActivateOptions() to notify the logging
                //sub system that the configuration for this appender has changed.
                fileAppender.ActivateOptions();
            }
        }
    }
}
